package com.hygj.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hygj.pojo.RandomTest;
import com.hygj.service.RandomTestService;
import com.hygj.util.PageUtil;

@Controller
@RequestMapping
public class RandomTestController {

	@Autowired
	private RandomTestService randomTestService;

	/**
	 * 保存
	 * 
	 * @param randomTest
	 * @return
	 */
	@RequestMapping(value = "insert", method = RequestMethod.GET)
	public String insertGet(RandomTest randomTest) {

		return "random/randomPrintOne";
	}

	@RequestMapping(value = "insert", method = RequestMethod.POST)
	public String insertPost(Map<String, Object> map, RandomTest randomTest, HttpServletRequest request) {
		randomTestService.insert("insert", randomTest);
		map.put("randomTest", randomTest);
		/*String url = request.getRequestURI().replaceFirst(request.getContextPath(), "");*/
		// 去除jsessionid，用于和后台地址比对
		/*if (url.contains(";")) {
			url = url.substring(0, url.indexOf(";"));
		}
		map.put("url", url);*/
		return "random/randomPrintOne";
	}

	

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("delete")
	public String delete(@RequestParam("id") Integer id) {
		randomTestService.delete("delete", id);

		return "redirect:getAll";
	}

	/**
	 * 查询全部
	 * 
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping("getAll")
	public String getAll(@ModelAttribute("search") RandomTest randomTest, Map<String, Object> map,
			HttpServletRequest request) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 判断长度，是否为空
		if (StringUtils.isNotBlank(randomTest.getMissionName())) {
			params.put("missionName", "%" + randomTest.getMissionName() + "%");

		}
		PageUtil<RandomTest> page = randomTestService.page(request, 10, params);
		map.put("page", page);
		// String url =
		// request.getRequestURI().replaceFirst(request.getContextPath(), "");
		// 去除jsessionid，用于和后台地址比对
		/// if (url.contains(";")) {
		// url = url.substring(0, url.indexOf(";"));
		// }
		// map.put("url", url);
		return "random/randomShow";
	}

	/**
	 * 详情页面
	 * 
	 * @param id
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("getById")
	public String getById(@RequestParam("id") Integer id, ModelMap modelMap) {
		RandomTest randomTests = randomTestService.getById("getById", id);
		modelMap.addAttribute("randomTests", randomTests);
		return "random/randomDetails";
	}

	/**
	 * 打印页面
	 * 
	 * @param id
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("getByIds")
	public String getByIds(@RequestParam("id") Integer id, ModelMap modelMap) {
		RandomTest randomTests = randomTestService.getById("getById", id);
		modelMap.addAttribute("randomTests", randomTests);
		return "random/randomPrint";
	}
}
